package com.shanling.shanlingcontroller.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chipsguide.lib.bluetooth.entities.BluetoothDeviceMusicSongEntity;
import com.chipsguide.lib.bluetooth.managers.BluetoothDeviceMusicManager;
import com.shanling.shanlingcontroller.CustomApplication;
import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.adapter.CommonAdapter;
import com.shanling.shanlingcontroller.adapter.ViewHolder;
import com.shanling.shanlingcontroller.base.BaseAppManager;
import com.shanling.shanlingcontroller.bean.Music;
import com.shanling.shanlingcontroller.manager.DeviceMusicManager;
import com.shanling.shanlingcontroller.music.player.PlayManager;
import com.shanling.shanlingcontroller.music.player.playqueue.PlayQueueManager;
import com.shanling.shanlingcontroller.ui.activity.DeviceMusicPlayActivity;
import com.shanling.shanlingcontroller.ui.activity.MusicPlayActivity;
import com.shanling.shanlingcontroller.utils.DpUtils;
import com.shanling.shanlingcontroller.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaobai on 2019/12/12.
 */

public class DeviceMusicListPopup {
    private CommonAdapter<BluetoothDeviceMusicSongEntity> musicCommonAdapter;
    private PopupWindow popupWindow;
    private View popupView;
    private Activity activity;
    private List<BluetoothDeviceMusicSongEntity> mEntityList;

    public DeviceMusicListPopup(Activity activity) {
        this.activity = activity;
        mEntityList = new ArrayList<>();
        initPopWindow(activity);
    }

    private void initPopWindow(Context mContext) {
        popupView = LayoutInflater.from(mContext).inflate(R.layout.music_device_list_popup, null);
        popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, DpUtils.dpToPx( mContext,350), true);
        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setAnimationStyle(R.style.PopupAnimation);
        popupWindow.setBackgroundDrawable(new ColorDrawable(mContext.getResources().getColor(R.color.bg_normal)));
        popupWindow.getContentView().setFocusableInTouchMode(true);
        popupWindow.getContentView().setFocusable(true);
        musicCommonAdapter = new CommonAdapter<BluetoothDeviceMusicSongEntity>(mContext, mEntityList, R.layout.item_musiclistpop) {
            @Override
            public void convert(ViewHolder helper, int position, BluetoothDeviceMusicSongEntity item) {
                TextView tv_name = helper.getView(R.id.tv_name);
                ImageView iv_delete=helper.getView(R.id.iv_delete);
                iv_delete.setVisibility(View.INVISIBLE);
                tv_name.setText(item.getName());
                if(DeviceMusicManager.getInstance().getCurrentSong().getName().equals(item.getName())){
                    tv_name.setTextColor(0xffCAB487);
                }else {
                    tv_name.setTextColor(0xffffffff);
                }
                tv_name.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        BluetoothDeviceMusicSongEntity entity = mEntityList.get(position);
                        DeviceMusicManager.getInstance().selectPlay(entity.getIndex());
                        DeviceMusicPlayActivity.launch(activity, entity);
                        popupWindow.dismiss();
                    }
                });
            }
        };
    }

    public void show(List<BluetoothDeviceMusicSongEntity> entityList, View view){
        if(popupView != null && view != null){
            ListView iv_cover = popupView.findViewById(R.id.lv_songs);
            ImageView iv_mode = popupView.findViewById(R.id.iv_mode);
            TextView tv_mode = popupView.findViewById(R.id.tv_mode);
            int playMode = DeviceMusicManager.getInstance().getCurrentLoopMode();
            switch (playMode){
                case BluetoothDeviceMusicManager.LoopMode.SINGLE:
                    iv_mode.setImageResource(R.drawable.player_icon_single);
                    tv_mode.setText(R.string.play_mode_repeat);
                    break;
                case BluetoothDeviceMusicManager.LoopMode.SHUFFLE:
                    iv_mode.setImageResource(R.drawable.player_icon_shuffle);
                    tv_mode.setText(R.string.play_mode_random);
                    break;
                case BluetoothDeviceMusicManager.LoopMode.ALL:
                    iv_mode.setImageResource(R.drawable.player_icon_list_circulation);
                    tv_mode.setText(R.string.play_mode_loop);
                    break;
            }
            mEntityList = entityList;
            musicCommonAdapter.refresh(entityList);
            iv_cover.setAdapter(musicCommonAdapter);
            popupWindow.showAtLocation(view, Gravity.BOTTOM,0,0);
            bgAlpha(0.4f,activity);
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    bgAlpha(1.0f,activity);
                }
            });
        }
    }

    private  void bgAlpha(float f,Activity activity){
        WindowManager.LayoutParams layoutParams = activity.getWindow().getAttributes();
        layoutParams.alpha = f;
        activity.getWindow().setAttributes(layoutParams);
    }
}
